<?php
/**
 * 总控，是否为空控制器，空操作，是否登录
 * 
 * @author Lee<a605333742@gmail.com>
 * @date    2018-06-26
 */
namespace app\admin\controller;

use think\Controller;
use app\admin\model\Node;
use app\admin\model\Role;
use think\Cache;
use think\Request;

class Common extends Controller{
    protected $nodeModel;
    protected $roleModel;
    protected $oneNodeList;
    protected $twoNodeList;
    protected $request;
    protected $data;
    protected $list;
    public function _initialize() {
        parent::_initialize();
        if(session('is_login')==NULL){
            $this->redirect('Login/index');
        }
        $this->nodeModel=New Node();
        $this->roleModel=new Role();
        $this->request  = Request::instance();
        $this->data     =$this->request->param();
        $this->_initCondition();
    }
    
    /*
     * 初始化左侧菜单
     * 
     * @return #
     */
    public function _initCondition(){
        $condition['status']=1;
        if(session('user.name')!='admin'){
            //不是超级管理员，查询出角色名称和所拥有的节点ID
            $node_ids=$this->roleModel->getNodedList(array('role_id'=> session('roleInfo.role_id')),'node_id');
            $nodes=array();
            foreach($node_ids as $vo){
                $nodes[]=$vo;
            }
            $condition['id']=array("IN",implode(',', $nodes));
        }        
        if(!Cache::get('oneNodeList')){  
            //顶级节点
            $condition['level']=0;            
        }
        Cache::set('oneNodeList',$this->nodeModel->getNodeList($condition,1));
        if(!Cache::get("twoNodeList")){
            //次级节点
            $condition['level']=1;            
        }
        Cache::set('twoNodeList',$this->nodeModel->getNodeList($condition,1));
        $this->oneNodeList=Cache::get('oneNodeList');
        $this->twoNodeList=Cache::get('twoNodeList');        
        $this->assign("oneNodeList",$this->oneNodeList);
        $this->assign("twoNodeList",$this->twoNodeList);
        $this->assign('controller',$this->request->controller());
        $this->assign('data',$this->data);
    }
}
